package ro.k.tags;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import ro.k.jstore.ManoperaBaseService;
import ro.k.web.beans.KBeanUtils;
import ro.k.web.beans.order.ManoperaBaseBean;

public class DBManopera implements Serializable {

	private static final long serialVersionUID = 454749423657944553L;
	private List<ManoperaBaseBean> manopera = new ArrayList<ManoperaBaseBean>();
	private String codeFilter;
	private String nameFilter;
	private String sortColumn = "n";
	private boolean sortAsc;
	private boolean hidden = false;
	public static String HIDE_TRUE = "T";
	public static String HIDE_FALE = "F";

	public DBManopera() {
		manopera = ManoperaBaseService.getInstance().getManoperaBase();
	}

	public void setSort(Object obj) {
		if (KBeanUtils.isEmpty(obj)) {
			return;
		}
		String newSortCol = obj.toString();
		if (newSortCol.equals(sortColumn)) {
			sortAsc = !sortAsc;
		} else {
			sortColumn = newSortCol;
			sortAsc = false;
		}
	}

	private List<ManoperaBaseBean> filterManopera() {
		Set<ManoperaBaseBean> set = new HashSet<ManoperaBaseBean>();
		boolean filterOn = false;
		if ((codeFilter != null && codeFilter.length() > 0)
				|| nameFilter != null && nameFilter.length() > 0) {
			filterOn = true;
		}
		for (ManoperaBaseBean mbb : manopera) {
			if (codeFilter != null && codeFilter.length() > 0) {
				if (mbb.getCod().toString().contains(codeFilter)) {
					set.add(mbb);
				}
			} else {
				if (!filterOn) {
					set.add(mbb);
				}
			}
			if (nameFilter != null && nameFilter.length() > 0) {
				if (mbb.getNume().contains(nameFilter)) {
					set.add(mbb);
				}
			} else {
				if (!filterOn) {
					set.add(mbb);
				}
			}
		}
		List<ManoperaBaseBean> li = new ArrayList<ManoperaBaseBean>(set);
		return li;
	}

	public List<ManoperaBaseBean> returnManopera() {
		List<ManoperaBaseBean> li = filterManopera();
		if (!sortAsc) {
			if (sortColumn.equals("n")) {
				Collections.sort(li, new CompareName());
			}
			if (sortColumn.equals("c")) {
				Collections.sort(li, new CompareCode());
			}
		} else {
			if (sortColumn.equals("n")) {
				Collections.sort(li,
						Collections.reverseOrder(new CompareName()));
			}
			if (sortColumn.equals("c")) {
				Collections.sort(li,
						Collections.reverseOrder(new CompareCode()));
			}
		}
		return li;
	}

	class CompareName implements Comparator<ManoperaBaseBean> {
		@Override
		public int compare(ManoperaBaseBean o1, ManoperaBaseBean o2) {
			return o1.getNume().compareTo(o2.getNume());
		}
	}

	class CompareCode implements Comparator<ManoperaBaseBean> {
		@Override
		public int compare(ManoperaBaseBean o1, ManoperaBaseBean o2) {
			return o1.getCod().compareTo(o2.getCod());
		}
	}

	public List<ManoperaBaseBean> getManopera() {
		return manopera;
	}
	
	public ManoperaBaseBean getManoperaByCode(String code){
		for(ManoperaBaseBean mbb : manopera){
			if(mbb.getCod().equals(code)){
				return mbb;
			}
		}
		return null;
	}

	public boolean setHidden(Object hiddObj) {
		if (hiddObj != null) {
			if (hiddObj.toString().equals(HIDE_TRUE)) {
				hidden = true;
			} else {
				hidden = false;
			}
		}
		return hidden;
	}

	// custom add
	public void newAdd(ManoperaBaseBean mbb) {
		// comandaManopera.add(mbb);
	}

	public String getCodeFilter() {
		return codeFilter;
	}

	public void setCodeFilter(String codeFilter) {
		this.codeFilter = codeFilter;
	}

	public String getNameFilter() {
		return nameFilter;
	}

	public void setNameFilter(String nameFilter) {
		this.nameFilter = nameFilter;
	}

	public boolean isSortAsc() {
		return sortAsc;
	}

	public void setSortAsc(boolean sortAsc) {
		this.sortAsc = sortAsc;
	}

	public boolean isName() {
		return sortColumn.equals("n");
	}

	public boolean isCode() {
		return sortColumn.equals("c");
	}

	public void setCodeFilter(Object codeFilter) {
		if (codeFilter != null) {
			this.codeFilter = codeFilter.toString();
		}
	}

	public void setNameFilter(Object nameFilter) {
		if (nameFilter != null) {
			this.nameFilter = nameFilter.toString();
		}
	}

	public void clearFilters() {
		codeFilter = null;
		nameFilter = null;
	}

}
